package gulimall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 跨域配置类
 * @author HLF
 * @date 2022年4月25日16:40:31
 */
@Configuration
public class GulimallCorsConfiguration {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConfiguration = new CorsConfiguration();

        //允许哪些请求头跨域
        corsConfiguration.addAllowedHeader("*");
        //允许哪些方法跨域 get post delete put ...
        corsConfiguration.addAllowedMethod("*");
        //允许哪些请求来源跨域  * 表示任意来源
        //corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedOriginPattern("*");
        //是否允许携带cookie跨域
        corsConfiguration.setAllowCredentials(true);

        //注册跨域配置  /**配置请求路径
        source.registerCorsConfiguration("/**", corsConfiguration);
        return new CorsWebFilter(source);
    }
}
